package com.xinyuan.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.xinyuan.entity.Check;
import com.xinyuan.entity.RespWork;
import com.xinyuan.service.WorkHourService;
import com.xinyuan.utils.AmecoUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.crypto.Data;
import java.io.IOException;

/**
 * @Classname UpdateMTOPExcelController
 * @Description TODO
 * @Date 2020/6/4 18:29
 * @Created by zfl
 */
@RestController
public class UpdateMTOPExcelController extends HttpServlet {
    @Autowired
    private WorkHourService workHourService;

    @PostMapping("/updateMTOPExcelOne")
    public void updateMTOPExcelOne(@RequestParam("id") String fileID, HttpServletRequest request, HttpServletResponse response) throws IOException {
        String filePath = "C:\\upload\\" + fileID + "\\MTOPONE.xlsx";
        updateMTOPExcel(fileID,request,response,filePath);
    }

    @PostMapping("/updateMTOPExcelTwo")
    public void updateMTOPExcelTwo(@RequestParam("id") String fileID, HttpServletRequest request, HttpServletResponse response) throws IOException {
        String filePath = "C:\\upload\\" + fileID + "\\MTOPTWO.xlsx";
        updateMTOPExcel(fileID,request,response,filePath);
    }

    public void updateMTOPExcel(String fileID, HttpServletRequest request, HttpServletResponse response, String filePath) throws IOException {
        response.addHeader("Access-Control-Allow-Origin","*");
        long t1 = System.currentTimeMillis();
        AmecoUtils utils = new AmecoUtils();
        String json = utils.acceptJSON(request);
        //System.out.println(json);
        ObjectMapper mapper = new ObjectMapper();
        Check check = new Check();
        RespWork respWork = new RespWork();
        //System.out.println("workHourService = " + workHourService);
        try {
            check = mapper.readValue(json,Check.class);
            respWork.setErrCode(0);
            respWork.setMsg("录入正确！");
            System.out.println(check);
            //Check finalCheck = check;
            utils.saveData(fileID,workHourService, check, filePath);
            long t2 = System.currentTimeMillis();
            System.out.println("用时：" + (t2 - t1) / 1000 + "秒");
            response.setContentType("application/json;charset=utf-8");
            response.getWriter().write(utils.getJson(respWork));
        } catch (JsonProcessingException e) {
            System.out.println(e);
            respWork.setErrCode(1);
            respWork.setMsg("录入错误！");
            respWork.setAwork(null);
            respWork.setCwork(null);
            //return utils.getJson(respWork);
            response.setContentType("application/json;charset=utf-8");
            response.getWriter().write(utils.getJson(respWork));
            System.out.println("格式转化异常");
            System.out.println(check);
        }
    }
}
